/**
 * Created by John on 2016/7/14.
 */
var demo = new Sample();

// demo.ue();
// demo.observer();
// demo.nextTick();
// demo.setImmediate();

function Sample() {
    this.ue = function () {
        getData('from_db');
        getData('from_remote_api');

        getData('from_db', function (result) {
            // ...
        });
        getData('from_remote_api', function (result) {
            // ...
        });
    };

    this.observer = function () {
        var forEach = function (list, callback) {
            for (var i = 0; i < list.length; i++) {
                callback(list[i], i, list);
            }
        };

        fs.open = function (path, flags, mode, callback) {
            binding.open(pathModule._makeLong(path),
                stringToFlags(flags),
                mode,
                callback
            );
        };
    };

    this.nextTick = function (callback) {
        // on the way out, don't bother.
        // it won't get fired anyway.
        if (process._exiting) {
            return;
        }

        var tock = {callback: callback};
        if (process.domain) {
            tock.domain = process.domain;
        }
        nextTickQueue.push(tock);
        if (nextTickQueue.length) {
            process._needTickCallback();
        }
    };

    this.setImmediate = function () {
        function exec1() {
            process.nextTick(function () {
                console.log('延迟执行');
            });
            console.log('正常执行');

            setImmediate(function () {
                console.log('延迟执行');
            });
            console.log('正常执行');
        }

        function exec2() {
            process.nextTick(function () {
                console.log('nextTick 延迟执行');
            });
            setImmediate(function () {
                console.log('setImmdeiate 延迟执行');
            });
            console.log('正常执行');
        }

        function exec3() {
            process.nextTick(function () {
                console.log('nextTick 延迟执行1');
            });
            process.nextTick(function () {
                console.log('nextTick 延迟执行2');
            });
            setImmediate(function () {
                console.log('setImmdeiate 延迟执行1');
                process.nextTick(function () {
                    console.log('强势插入');
                })
            });
            setImmediate(function () {
                console.log('setImmdeiate 延迟执行2');
            });
            console.log('正常执行');
        }

        function exec4() {
            console.log('%value', '---------');
        }

        exec1();
        exec2();
        exec3();
        exec4();
    }
}


